<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/5/30
 * Time: 15:11
 * Desc: 门店自营商品管理
 */
namespace app\kuguan\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Storegoods extends BasicAdmin{
    /**
     * @Author  lingyun
     * @Desc    门店自营商品管理
     */
    public function index(){
        $this->title = '店内自营商品';
        $get = $this->request->get();
        $map = [];

        if(!empty($get['name'])){
            $map['a.name'] = ['like',"%".$get['name']."%"];
        }

//        foreach (['name'] as $key) {
//            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
//        }
        $join = [
            ['ys_goods_spec_detail b','a.id=b.goods_id','LEFT'],
            ['ys_store c','a.store_id=c.id','LEFT']
        ];


        $rows = intval($this->request->get('rows', cookie('rows')));
        cookie('rows', $rows >= 10 ? $rows : 20);
        $page = db('goods')->alias('a')->join($join)->where(['a.is_deleted'=>['eq',0],'b.is_checked'=>['neq',0]])->field('a.id,a.name,a.logo,b.price,b.id as spec_id,b.member_price,b.peifang_ids,b.is_checked,c.title')->where($map)->paginate($rows, false, ['query' => request()->get('', '', 'urlencode')]);
        list($pattern, $replacement) = [['|href="(.*?)"|', '|pagination|'], ['data-open="$1"', 'pagination pull-right']];
        list($result['list'], $result['page']) = [$page->all(), preg_replace($pattern, $replacement, $page->render())];

        foreach($result['list'] as $k => $v){
            $result['list'][$k]['chengben'] = db('goods_peifang')->where('id','in',$v['peifang_ids'])->sum('chengben');
        }

        $this->assign('title',$this->title);            //必须加上title,不然顶部的按钮不会显示
        return $this->fetch('index', $result);
    }

    /**
     * @Author  lingyun
     * @Desc    配方
     * @return array|string
     */
    public function peifang(){
        $id = input('id');      //商品id
        $peifang_ids = db('goods_spec_detail')->where('goods_id',$id)->value('peifang_ids');
        $this->title = '配方列表';

        $db = db('goods_peifang')->where('id','in',$peifang_ids);

        return parent::_list($db);
    }

    /**
     * @Author  lingyun
     * @Desc    配方详情
     * 2018-05-30
     */
    public function peifang_detail(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            $data = $this->create_data_arr($post);
            db('goods_peifang_yongliao')->where(['p_id'=>$post['p_id']])->update(['is_deleted'=>1]);
            $model = new GoodsPeifangYongliao();
            $res = $model->saveall($data['data']);
            db('goods_peifang')->where('id',$post['p_id'])->update(['chengben'=>$data['chengben'],'chengben_price'=>$data['chengben_price']]);
            $this->success('保存成功',url('@admin').'#'.url('Peifang/index').'?spm='.input('spm'));
        }else{
            $id = input('id');          //ys_goods_peifang --- id           //商品属性id
            $peifang = db('goods_peifang')->where('id',$id)->find();
//            $goods = db('goods_spec_detail')->where('id',$id)->find();
            $list = db('goods_peifang_yongliao')->where(['p_id'=>$id,'is_deleted'=>0])->select();
            $material = db('goods_material')->where(['is_deleted'=>0,'store_id'=>0])->select();

            $this->assign('id',$id);
//            $this->assign('goods',$goods);
            $this->assign('peifang',$peifang);
            $this->assign('title','编辑配方 -- '.$peifang['title']);
            $this->assign('list',$list);                //用料详情
            $this->assign('material',$material);        //原材料
            if(!empty($list)){
                $this->assign('url',url('Peifang/update_peifang'));        //保存配方
            }else{
                $this->assign('url',url('Peifang/add_edit_peifang'));      //保存配方
            }

            return $this->fetch('peifang_detail');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    审核店内商品
     *          新增门店商品库存入库数据
     * 2018-05-30
     */
    public function set_check(){
        $id = input('id');
        $value = input('value');
        $goods = db('goods_spec_detail')->where('id',$id)->find();          //商品信息

        $data_arr['goods_id'] = $goods['goods_id'];
        $data_arr['goods_spec_id'] = $id;
        $data_arr['spec_title'] = $goods['title'];
        $data_arr['price'] = $goods['price'];
        $data_arr['specs'] = $goods['specs'];
        $data_arr['onsale'] = 0;
        $data_arr['store_id'] = $goods['store_id'];
        $data_arr['is_top'] = 0;
        $data_arr['trim_price'] = '';
        $data_arr['warn_stock'] = 0;
        $data_arr['stock'] = 0;
        $data_arr['addtime'] = time();
        $data_arr['is_diannei'] = 1;

        $sql = "select sum(chengben_price) as signal_chengben ,sum(chengben) as s_chengben from ys_goods_peifang where id in ('".$goods['peifang_ids']."')";
        $chengben = Db::query($sql);

        $data_arr['chengben'] = $chengben[0]['s_chengben'];
        $data_arr['chengben_price'] = $chengben[0]['signal_chengben'];

        if($value == 2){            //审核通过，新增库存记录
            $data_arr['is_deleted'] = 0;
//            $data_arr = [
//                'goods_id'=>$goods['goods_id'],
//                'goods_spec_id'=>$id,
//                'spec_title'=>$goods['title'],
//                'price'=>$goods['price'],
//                'specs'=>$goods['specs'],
//                'onsale'=>0,
//                'store_id'=>goods['store_id'],
//                'is_top'=>0,
//                'trim_price'=>0,
//                'warn_stock'=>0,
//                'stock'=>0,
//                'addtime'=>time(),
//                'is_deleted'=>0,
//                'is_diannei'=>1
//            ];
        }else{
            $data_arr['is_deleted'] = 1;
        }
        $flag = db('store_goods_stock')->insert($data_arr);      //新增门店商品库存数量

        $res = db('goods_spec_detail')->where('id',$id)->update(['is_checked'=>$value]);        //更新属性
        $res!==false?$this->success('审核成功',''):$this->error('审核失败','');
    }


}